import request from '../utils/request'
import global from '../utils/global'
/**
 * 文件上传API
 */
export default{

    //获取上传upToken
    getUpToken:function(){
        return request({
            url:'/hnister-file-service/upToken',
            method:'get'
        })
    },
    //覆盖上传方式
    getUpToken:function(key){
        return request({
            url:'/hnister-file-service/upToken',
            method:'get',
            params:{
                key:key
            }
        })
    },
    //以表单方式上传文件 参考:
    //https://developer.qiniu.com/kodo/manual/1272/form-upload
    upload: function(fileForm,upToken,key,otherOpt){
        if (!otherOpt){
            otherOpt = {}
        }

        fileForm.append('key',key)
        fileForm.append('token',upToken)
        for(k in Object.keys(otherOpt)){
            fileForm.append(k,otherOpt[k])
        }
        return request({
            url:global.address.qiniuUploadURL,
            baseURL:'',
            method:'post',
            data:fileForm,
            headers:{'Content-Type':'multipart/form-data'}
        })
    },
    //上传后返回的key 获取文件外链
    getDownloadURL:function(key){
        return global.address.qiniuDownloadURL + '/' + key;
    }
}